//
//  NSString+LCExtension.m
//  LCPackage
//
//  Created by 马军凯 on 2021/6/22.
//  Copyright © 2021 龙采科技集团. All rights reserved.
//

#import "NSString+LCExtension.h"

@implementation NSString (LCExtension)
//MARK: ========================================正则========================================
@dynamic CrazyValidatePhoneNum;
@dynamic CrazyValidateIdentityCard;

//手机验证
- (BOOL)CrazyValidatePhoneNum {
    if (self.length != 11) {
        return NO;
    } else {
        NSString *num = @"^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(16[5,6])|(17[1,3-8])|(18[0-9])|(19[8,9]))\\d{8}|((1705)|(1709))\\d{7}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", num];
        BOOL isMatch = [pred evaluateWithObject:self];
        NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(172)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
        NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(175)|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
        NSString *CT_NUM = @"^((133)|(149)|(153)|(171)|(173)|(174)|(177)|(166)|(199)|(18[0,1,9]))\\d{8}$";
        NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
        BOOL isMatch1 = [pred1 evaluateWithObject:self];
        NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
        BOOL isMatch2 = [pred2 evaluateWithObject:self];
        NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
        BOOL isMatch3 = [pred3 evaluateWithObject:self];
        if (isMatch1 || isMatch2 || isMatch3 || isMatch) {
            return YES;
        } else {
            return NO;
        }
    }
}

//身份证号
- (BOOL)CrazyValidateIdentityCard {
    NSString * identityCard = self;
    //判断位数
    if ([identityCard length] != 15 && [identityCard length] != 18) {
        return NO;
    }
    NSString *carid = identityCard;
    long lSumQT = 0;
    //加权因子
    int R[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
    //校验码
    unsigned char sChecker[11] = {'1','0','X', '9', '8', '7', '6', '5', '4', '3', '2'};
    //将15位身份证号转换成18位
    NSMutableString *mString = [NSMutableString stringWithString:identityCard];
    if ([identityCard length] == 15) {
        [mString insertString:@"19" atIndex:6];
        long p = 0;
        const char *pid = [mString UTF8String];
        for (int i = 0; i <= 16; i++) {
            p += (pid[i] - 48) * R[i];
        }
        int o = p % 11;
        NSString *string_content = [NSString stringWithFormat:@"%c",sChecker[o]];
        [mString insertString:string_content atIndex:[mString length]];
        carid = mString;
    }
    //判断地区码
    NSString *sProvince = [carid substringToIndex:2];

    if (![self areaCode:sProvince]) {
        return NO;
    }
    //判断年月日是否有效
    //年份
    int strYear = [[self getStringWithRange:carid Value1:6 Value2:4] intValue];
    //月份
    int strMonth = [[self getStringWithRange:carid Value1:10 Value2:2] intValue];
    //日
    int strDay = [[self getStringWithRange:carid Value1:12 Value2:2] intValue];
    
    NSTimeZone *localZone = [NSTimeZone localTimeZone];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]  ;
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [dateFormatter setTimeZone:localZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%d-%d 12:01:01", strYear, strMonth, strDay]];
    if (date == nil) {
        return NO;
    }
    
    const char *PaperId  = [carid UTF8String];
    //检验长度
    if (18 != strlen(PaperId)) return NO;
    //校验数字
    for (int i = 0; i < 18; i++) {
        if (!isdigit(PaperId[i]) && !(('X' == PaperId[i] || 'x' == PaperId[i]) && 17 == i)) {
            return NO;
        }
    }
    //验证最末的校验码
    for (int i = 0; i <= 16; i++) {
        lSumQT += (PaperId[i] - 48) * R[i];
    }
    if (sChecker[lSumQT % 11] != PaperId[17]) {
        return NO;
    }
    
    return YES;
}

- (BOOL)areaCode:(NSString *)code {
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init] ;
    [dic setObject:@"北京" forKey:@"11"];
    [dic setObject:@"天津" forKey:@"12"];
    [dic setObject:@"河北" forKey:@"13"];
    [dic setObject:@"山西" forKey:@"14"];
    [dic setObject:@"内蒙古" forKey:@"15"];
    [dic setObject:@"辽宁" forKey:@"21"];
    [dic setObject:@"吉林" forKey:@"22"];
    [dic setObject:@"黑龙江" forKey:@"23"];
    [dic setObject:@"上海" forKey:@"31"];
    [dic setObject:@"江苏" forKey:@"32"];
    [dic setObject:@"浙江" forKey:@"33"];
    [dic setObject:@"安徽" forKey:@"34"];
    [dic setObject:@"福建" forKey:@"35"];
    [dic setObject:@"江西" forKey:@"36"];
    [dic setObject:@"山东" forKey:@"37"];
    [dic setObject:@"河南" forKey:@"41"];
    [dic setObject:@"湖北" forKey:@"42"];
    [dic setObject:@"湖南" forKey:@"43"];
    [dic setObject:@"广东" forKey:@"44"];
    [dic setObject:@"广西" forKey:@"45"];
    [dic setObject:@"海南" forKey:@"46"];
    [dic setObject:@"重庆" forKey:@"50"];
    [dic setObject:@"四川" forKey:@"51"];
    [dic setObject:@"贵州" forKey:@"52"];
    [dic setObject:@"云南" forKey:@"53"];
    [dic setObject:@"西藏" forKey:@"54"];
    [dic setObject:@"陕西" forKey:@"61"];
    [dic setObject:@"甘肃" forKey:@"62"];
    [dic setObject:@"青海" forKey:@"63"];
    [dic setObject:@"宁夏" forKey:@"64"];
    [dic setObject:@"新疆" forKey:@"65"];
    [dic setObject:@"台湾" forKey:@"71"];
    [dic setObject:@"香港" forKey:@"81"];
    [dic setObject:@"澳门" forKey:@"82"];
    [dic setObject:@"国外" forKey:@"91"];
    
    if ([dic objectForKey:code] == nil) {
        return NO;
    }
    return YES;
}

- (NSString *)getStringWithRange:(NSString *)str Value1:(int)value1 Value2:(int )value2 {
    return [str substringWithRange:NSMakeRange(value1, value2)];
}
//MARK: ========================================方法========================================
// textField的placeHolder
+ (NSAttributedString *)textFieldPlaceHolderStringWithText:(NSString *)text font:(UIFont *)font color:(UIColor *)color {
    return [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: font, NSForegroundColorAttributeName: color}];
}
// 给label划中线
+ (NSMutableAttributedString *)stringMidLine:(NSString *)string {
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc] initWithString:string attributes:attribtDic];
    return attribtStr;
}
//修改某一段的字体和颜色
+ (NSMutableAttributedString *)changeSectionFontAndColorWithString:(NSString *)text font:(UIFont *)font color:(UIColor *)color from:(NSInteger)from lenth:(NSInteger)lenth {
    
    return [NSString changeSectionFontAndColorWithString:text font:font color:color fontFrom:from fontLenth:lenth colorFrom:from colorLenth:lenth];
}
//改变某一段的字体大小
+ (NSMutableAttributedString *)changeSectionFontWithString:(NSString *)text font:(UIFont *)font from:(NSInteger)from lenth:(NSInteger)lenth {
    
    return [NSString changeSectionFontAndColorWithString:text font:font color:[UIColor whiteColor] fontFrom:from fontLenth:lenth colorFrom:0 colorLenth:0];
}
//修改某一段的字体颜色
+ (NSMutableAttributedString *)changeSectionColorWithString:(NSString *)text color:(UIColor *)color from:(NSInteger)from lenth:(NSInteger)lenth {
    
    return [NSString changeSectionFontAndColorWithString:text font:[UIFont systemFontOfSize:0] color:color fontFrom:0 fontLenth:0 colorFrom:from colorLenth:lenth];
}
//修改某一段的字体颜色和大小
+ (NSMutableAttributedString *)changeSectionFontAndColorWithString:(NSString *)text font:(UIFont *)font color:(UIColor *)color fontFrom:(NSInteger)fontFrom fontLenth:(NSInteger)fontLenth colorFrom:(NSInteger)colorFrom colorLenth:(NSInteger)colorLenth {
    
    NSMutableAttributedString *textString = [[NSMutableAttributedString alloc] initWithString:text];
    [textString addAttribute:NSFontAttributeName value:font range:NSMakeRange(fontFrom, fontLenth)];
    [textString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(colorFrom, colorLenth)];
    
    return textString;
}
//把手机号中间四位变成****
+ (NSString *)crazyReplaceStar:(NSString *)str {
    NSString *aStr = [str substringWithRange:NSMakeRange(0, 3)];
    NSString *cStr = [str substringWithRange:NSMakeRange(7, 4)];
    NSString *replacedStr = [NSString stringWithFormat:@"%@****%@", aStr, cStr];
    return replacedStr;
}

//去除空格
+ (NSString *)deleteSpaceWith:(NSString *)str {
    NSString *text = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    text = [text stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    text = [text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
    return text;
}

//时间转换成时间戳   2018-09-26 -> 1537949271
+ (NSString *)timeChangeTimeString:(NSString *)time {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSDate *date = [formatter dateFromString:time];
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    
    return [NSString stringWithFormat:@"%ld", (long)timeSp];
}
//详细时间转换成时间戳   2018-09-26 16:07:51 -> 1537949271
+ (NSString *)detailTimeChangeTimeString:(NSString *)time {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSDate *date = [formatter dateFromString:time];
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    
    return [NSString stringWithFormat:@"%ld", (long)timeSp];
}
//时间戳转换成时间  1537949271 -> 2018-09-26
+ (NSString *)timeStringChangeTime:(NSString *)timeString  {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSTimeInterval time = [timeString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    
    return [formatter stringFromDate:date];
}
//时间戳转换成详细时间  1537949271 -> 2018-09-26 16:07:51
+ (NSString *)timeStringChangeDetailTime:(NSString *)timeString {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSTimeInterval time = [timeString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    
    return [formatter stringFromDate:date];
}
//当前时间转换成时间戳
+ (NSString *)currentTimeChangeTimeString {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSDate *nowDate = [NSDate date];
    NSInteger timeSp = [[NSNumber numberWithDouble:[nowDate timeIntervalSince1970]] integerValue];
    
    return [NSString stringWithFormat:@"%ld", (long)timeSp];
}

//视频第一帧
- (NSString *)videoFirstFrame {
//    NSString *firstString = @"?x-oss-process=video/snapshot,t_10000,m_fast";
    NSString *firstString = @"?x-oss-process=video/snapshot,t_0,f_jpg,w_0,h_0,m_fast,ar_auto";
    return [NSString stringWithFormat:@"%@%@", self, firstString];
}

@end
